//
//  MineHomepageViewController.m
//  ArtDog
//
//  Created by dllo on 16/8/29.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MineHomepageViewController.h"
#import "BaseTableView.h"
#import "MineHomepageTopTableCell.h"
#import "MineHomepageBodyTableCell.h"
#import "MineHomepageModel.h"
#import "HLMacro.h"
#import "ZWYNetTool.h"
#import "NSString+Common.h"
#import "HLMacro.h"
#import "MineAlterInfoViewController.h"
#import "MineSettingViewController.h"
#import "MineCollectViewController.h"

@interface MineHomepageViewController ()

@property (weak, nonatomic) IBOutlet BaseTableView *tableView;
@property (nonatomic, strong) NSArray *bodyArray;

@end

@implementation MineHomepageViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    self.hidesBottomBarWhenPushed = YES;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self tableViewProcotolMethod];
    [self setupTableView];
    [self handleData];
    [self changeTabBarController];
}

- (void)changeTabBarController
{
    self.navigationController.viewControllers = @[self];
}

- (void)handleData
{
    NSString *urlString = [NSString getURLStringWith:HOST andPaths:@[@"artgoer", @"api", @"v1", @"user", [NSString stringWithFormat:@"%@", _homePageModel.idField], @"baseInfoSecond"] andParams:@{@"token": _homePageModel.token, @"otherUserId": [NSString stringWithFormat:@"%@", _homePageModel.idField]}];
    [ZWYNetTool GET:urlString andBody:nil andHeader:@{@"Host": @"api.artgoer.cn:8084"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
        if ([result[@"status"] isEqualToString:@"200"]) {
            MineHomepageModel *model = [MineHomepageModel modelWithDic:result[@"data"]];
            _homePageModel = model;
            [_tableView reloadData];
        }
    } andFailureBlock:^(NSError *error) {
        NSLog(@"%@", error);
    }];
    
    _bodyArray = @[@"我的订单", @"我的收获地址", @"我的收藏", @"我的话题", @"消息和私信", @"设置", @"order", @"address", @"collect", @"topic", @"message", @"setting"];
}

#pragma mark - 第一个cell里面内容推出新界面
- (void)pushNewPage:(MineHomepageTopTableCell *)cell
{
    cell.infoEditBlock = ^(UIButton *button, MineHomepageModel *model) {
        MineAlterInfoViewController *alterInfoVC = [[MineAlterInfoViewController alloc]initWithNibName:@"MineAlterInfoViewController" bundle:[NSBundle mainBundle]];
        alterInfoVC.model = model;
        [self.navigationController pushViewController:alterInfoVC animated:YES];
    };
}


#pragma mark - tableView 的基本设置
- (void)setupTableView
{
    [_tableView registerNib:[UINib nibWithNibName:@"MineHomepageTopTableCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"mineHomepageTopTableCell"];
    [_tableView registerNib:[UINib nibWithNibName:@"MineHomepageBodyTableCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"mineHomepageBodyTableCell"];
}

#pragma mark - tableView 的协议方法
- (void)tableViewProcotolMethod
{
    [_tableView tableViewWithNumberOfRow:^(UITableView * _Nonnull tableView, TableViewReturnParam * _Nonnull tableViewParam) {
        tableViewParam.numberOfRow = _bodyArray.count / 2 + 1;
    } andCellForRowAtIndexPath:^(UITableView * _Nonnull tableView, NSIndexPath * _Nonnull indexPath, TableViewReturnParam * _Nonnull tableViewParam) {
        if (indexPath.row == 0) {
            MineHomepageTopTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mineHomepageTopTableCell"];
            cell.homepageModel = _homePageModel;
            [self pushNewPage:cell];
            tableViewParam.returnCell = cell;
        } else {
            MineHomepageBodyTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mineHomepageBodyTableCell"];
            [cell setCellWithModel:_homePageModel andArray:@[[NSString stringWithString:_bodyArray[indexPath.row - 1]], [NSString stringWithString:_bodyArray[_bodyArray.count / 2 + indexPath.row - 1]]]];
            tableViewParam.returnCell = cell;
        }
    } andHeightForrowAtIndexPath:^(UITableView * _Nonnull tableView, NSIndexPath * _Nonnull indexPath, TableViewReturnParam * _Nonnull tableViewParam) {
        if (indexPath.row == 0) {
            tableViewParam.heightAtRow = SCREEN_WIDTH / 2;
        } else {
            tableViewParam.heightAtRow = 60;
        }
    } andDidSelectRowAtIndexPath:^(UITableView * _Nonnull tableView, NSIndexPath * _Nonnull indexPath) {
        //self.hidesBottomBarWhenPushed = YES;
        if (indexPath.row == 6) {
            MineSettingViewController *settingVC = [[MineSettingViewController alloc]initWithNibName:@"MineSettingViewController" bundle:[NSBundle mainBundle]];
            
            [self.navigationController pushViewController:settingVC animated:YES];
        } else if (indexPath.row == 3) {
            MineCollectViewController *collectVC = [[MineCollectViewController alloc]init];
            [self.navigationController pushViewController:collectVC animated:YES];
        }
    }];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return SCREEN_WIDTH / 2;
    } else {
        return 60;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
